#!/bin/bash
# 
# python-3.10.0 下载地址：wget https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tgz
# openssl-1.1.1l 下载地址：curl -O https://www.openssl.org/source/openssl-1.1.1l.tar.gz
# 
# 安装pis:
# chmod +x python_install_script.sh && \cp -rf python_install_script.sh /usr/bin/pis
#
#


GIT_REPO=https://gitee.com/buyili/python-install-script.git
SOURCE_PATH=$HOME/.local/share/python-install-script
OPENSSL_INSTALL_PATH=/usr/local/openssl-1.1.1l
PYTHON_INSTALL_PATH=/usr/local/python-3.10
PROFILE_FILE=/etc/profile.d/python-3.10.sh


clone_script(){
        sudo git clone $GIT_REPO $SOURCE_PATH
        echo "Clone finished!\n"
}

check_clone(){
        if [[ -d $SOURCE_PATH ]]; then
                echo -e "Clone finished!\n"
        else 
                clone_script
        fi
}


##  安装openssl，因为python3.10 必须使用openssl1.1.1以上的版本。
install_openssl(){

        check_clone
        cd $SOURCE_PATH

        opensslStoreDir=`find /etc/ -name openssl.cnf -printf "%h\n"`
        echo "openssl store dir is ${opensslStoreDir}"

        tar xzf openssl-1.1.1l.tar.gz
        pushd openssl-1.1.1l
        ./config \
                --prefix=${OPENSSL_INSTALL_PATH} \
                --libdir=lib \
                --openssldir=${opensslStoreDir}

        make -j1 depend
        make -j8
        make install_sw
        popd
}



## 安装python
install_python(){

        check_clone
        cd $SOURCE_PATH

        tar xzf Python-3.10.0.tgz
        pushd Python-3.10.0
        ./configure -C \
                --with-openssl=${OPENSSL_INSTALL_PATH} \
                --with-openssl-rpath=auto \
                --prefix=${PYTHON_INSTALL_PATH} \
                --enable-optimizations
        make -j8
        make altinstall
        popd

        # ### 使用创建软连接的方式
        # ln -s ${PYTHON_INSTALL_PATH}/bin/python3.10 /usr/bin/python3.10
        # ln -s ${PYTHON_INSTALL_PATH}/bin/python3.10 /usr/bin/python3


        ###  使用修改环境变量的方式
        echo 'export PATH=$PATH:'${PYTHON_INSTALL_PATH}'/bin' >  $PROFILE_FILE
        if [[ ! $PATH =~ $PYTHON_INSTALL_PATH ]]; then
                source $PROFILE_FILE
        fi
}

install(){
        clone_script
        install_openssl
        install_python
}


help() {
    echo Python install tool
    echo ''
    echo -e if you have not clone source file, run \`pis clone\` first
#     echo -e if you have not installed pis, run \`pis install\` first
    echo ''
    echo Usage:
    echo -e "\t" "pis help"
    echo -e "\n" "Clone"
    echo -e "\t" "pis clone                     clone install source file"
    echo -e "\n" "Install/Uninstall"
    echo -e "\t" "pis install                   install openssl-1.1.1l and python-3.10.0"
    echo -e "\t" "pis install_openssl           only install openssl-1.1.1l"
    echo -e "\t" "pis install_python            only install python-3.10.0"
    echo -e "\t" "pis uninstall                 uninstall shadowsocksr client"
#     echo -e "\n" "Config and Subscribe"
#     echo -e "\t" "pis update       update subscription from $WEBSITE"
#     echo -e "\t" "pis config       edit config.json"
#     echo -e "\t" "pis xclip        paste configs from clipboard to config.json"
#     echo -e "\n" "Start/Stop/Restart"
#     echo -e "\t" "pis start        start the shadowsocks service"
#     echo -e "\t" "pis stop         stop the shadowsocks service"
#     echo -e "\t" "pis restart      restart the shadowsocks service"
#     echo -e "\n" "Testing and Maintenance"
#     echo -e "\t" "pis test         get ip from cip.cc using socks5 proxy"
#     echo -e "\t" "pis log          cat the log of shadowsocks"
#     echo -e "\t" "pis shell        cd into pis installation dir"
#     echo -e "\t" "pis clean        clean pis configuration backups"
}


case $1 in
    clone)                      clone_script                 ;;
    install)                    install                 ;;
    install_openssl)            install_openssl         ;;
    install_python)             install_python          ;;
    *)                          help                    ;;
esac

